<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <consult-item v-for="item in list" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<script setup lang="ts">
import { getConsultList } from '@/api/consult'
import ConsultItem from './consult-item.vue'
import { reactive, ref } from 'vue'
import { ConsultTypeEnum } from '@/enums'
import type { ConsultOrderItem } from '@/types/consult'

const props = defineProps<{
  type: ConsultTypeEnum
}>()

const params = reactive({
  current: 1,
  pageSize: 10,
  type: props.type
})

const loading = ref(false)
const finished = ref(false)
const list = ref<ConsultOrderItem[]>([])

const onLoad = async () => {
  try {
    // 1. 调接口
    const res = await getConsultList(params)
    // 2. 更新数据
    list.value.push(...res.data.rows)
    // 3. loading 改为false
    loading.value = false
    // 4. 判断当前是否已经拿完所有数据，如果是则更新finished 为 true ，否则更新页码+1
    if (list.value.length >= res.data.total) {
      finished.value = true
    } else {
      params.current++
    }
  } catch (error) {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
